# -*- Mode: Python -*- vi:si:et:sw=4:sts=4:ts=4:syntax=python
import shutil

from cerbero.utils import shell
from cerbero.tools.libtool import LibtoolLibrary

class Recipe(custom.GStreamer):
    name = 'gst-libav-1.0'
    # TODO - check license - plugin is certainly LGPLv2+, but need to check
    #        the linked libs
    licenses = [License.LGPLv2Plus]
    config_sh = 'sh ./autogen.sh --noconfigure && ./configure'
    configure_options = "--enable-lgpl --disable-examples --enable-static "
    remotes = {'origin': 'git://anongit.freedesktop.org/gstreamer/gst-libav'}
    commit = '7c304c0c1ed37cac2001aa9ecd7ebd6a8715a042'
    deps = ['gstreamer-1.0', 'gst-plugins-base-1.0', 'bzip2', 'zlib' ]

    files_plugins_codecs_restricted = ['lib/gstreamer-1.0/libgstlibav%(mext)s']
    files_plugins_codecs_restricted_devel = [
        'lib/gstreamer-1.0/libgstlibav.a', 'lib/gstreamer-1.0/libgstlibav.la',
        'lib/libavcodec.a', 'lib/libavcodec.la',
        'lib/libavformat.a', 'lib/libavformat.la',
        'lib/libavutil.a', 'lib/libavutil.la',
        'lib/libswresample.a', 'lib/libswresample.la',
        'lib/libavfilter.a', 'lib/libavfilter.la',
    ]

    def prepare(self):
        self.append_env['CFLAGS'] = " -Wno-error "
        self.append_env['CXXFLAGS'] = " -Wno-error "
        self.append_env['CPPFLAGS'] = " -Wno-error "

        if self.config.target_platform != Platform.LINUX:
            self.configure_options += ' --disable-gtk-doc'

        # Default AS is $CC, except iOS (set below)
        if Architecture.is_arm(self.config.target_arch):
            self.new_env = {'AS': os.environ.get('CC', '')}

        if self.config.target_platform == Platform.DARWIN:
            if self.config.target_arch == Architecture.X86_64:
                asflags = ' -arch x86_64 -m64'
            elif self.config.target_arch == Architecture.X86:
                asflags = ' -arch i386 -m32'
            self.configure_options += ' ASFLAGS="%s"' % asflags
        elif self.config.target_platform == Platform.ANDROID:
            if self.config.target_arch == Architecture.X86:
              # libav internally aligns stacks, while Android doesn't
              self.configure_options += ' --with-libav-extra-configure="--extra-cflags=\'-mincoming-stack-boundary=4\'"'
        elif self.config.target_platform == Platform.IOS:
            if Architecture.is_arm(self.config.target_arch):
                if 'GAS' in os.environ:
                    self.new_env = {'AS': os.environ['GAS']}
        if self.config.variants.nodebug:
            self.append_env['CFLAGS'] += ' -DGST_LEVEL_MAX=GST_LEVEL_FIXME'

        super(Recipe, self).prepare()


    def configure(self):
        super(recipe.Recipe, self).configure()

        libav_path = os.path.join(self.build_dir, 'gst-libs', 'ext', 'libav')
        if self.config.target_platform == Platform.WINDOWS:
            replacements = {'RANLIB=ranlib': 'RANLIB=%s' % os.environ['RANLIB'],
                            'RANLIB=%s-ranlib' % self.config.host: 'RANLIB=%s' % os.environ['RANLIB']}    
            shell.replace(os.path.join(libav_path, 'config.mak'), replacements)
        elif self.config.target_platform in [Platform.DARWIN, Platform.IOS]:
            if self.config.target_arch == Architecture.X86:
                replacements = {'HAVE_EBX_AVAILABLE=yes': 'HAVE_EBX_AVAILABLE=no',
                                'HAVE_EBX_AVAILABLE 1': 'HAVE_EBX_AVAILABLE 0',}
                shell.replace(os.path.join(libav_path, 'config.mak'), replacements)
                shell.replace(os.path.join(libav_path, 'config.h'), replacements)
            if self.config.target_platform == Platform.IOS:
                replacements = {'RANLIB=ranlib': 'RANLIB=%s' % os.environ['RANLIB'],
                                'RANLIB=%s-ranlib' % self.config.host: 'RANLIB=%s' % os.environ['RANLIB']}    
                shell.replace(os.path.join(libav_path, 'config.mak'), replacements)
        # log2 and log2f are not provided by bionic, but they are not checked
        # properly
        elif self.config.target_platform == Platform.ANDROID:
                replacements = {'HAVE_LOG2 1': 'HAVE_LOG2 0',
                                'HAVE_LOG2F 1': 'HAVE_LOG2F 0',}
                shell.replace(os.path.join(libav_path, 'config.h'), replacements)

